<?php

function getTree($data, $pid)
{
    $tree = [];
    foreach ($data as $k => $v) {
        if ($v['parent_id'] == $pid) {
            $treeData = getTree($data, $v['id']);
            if(!empty($treeData)){
                $v['data'] = $treeData;
            }else{
                $v['data'] = '';
            }
            $tree[] = $v;
        }
    }
    return $tree;
}

/**
 * @param string $url post请求地址
 * @param array $params
 * @return mixed
 */
function curl_post($url, array $params = array())
{
    $data_string = json_encode($params);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt(
        $ch, CURLOPT_HTTPHEADER,
        array(
            'Content-Type: application/json'
        )
    );
    $data = curl_exec($ch);
    curl_close($ch);
    return ($data);
}

function curl_post_raw($url, $rawData)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $rawData);
    curl_setopt(
        $ch, CURLOPT_HTTPHEADER,
        array(
            'Content-Type: text'
        )
    );
    $data = curl_exec($ch);
    curl_close($ch);
    return ($data);
}

/**
 * @param string $url get请求地址
 * @param int $httpCode 返回状态码
 * @return mixed
 */
function curl_get($url, &$httpCode = 0)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    //不做证书校验,部署在linux环境下请改为true
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    $file_contents = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return $file_contents;
}

function getRandChar($length)
{
    $str = null;
    $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
    $max = strlen($strPol) - 1;

    for ($i = 0;
         $i < $length;
         $i++) {
        $str .= $strPol[rand(0, $max)];
    }

    return $str;
}



function fromArrayToModel($m , $array)
{
    foreach ($array as $key => $value)
    {
        $m[$key] = $value;
    }
    return $m;
}

// 加密密码
function encryPassword($password,$salt){
    return md5($password.$salt);
}

// 生成uuid
function guid(){
    if (function_exists('com_create_guid')){
        $uuid = com_create_guid();
    }else{
        mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
        $charid = strtoupper(md5(uniqid(rand(), true)));
        $hyphen = chr(45);// "-"
        $uuid = chr(123)// "{"
                .substr($charid, 0, 8).$hyphen
                .substr($charid, 8, 4).$hyphen
                .substr($charid,12, 4).$hyphen
                .substr($charid,16, 4).$hyphen
                .substr($charid,20,12)
                .chr(125);// "}"
    }

    return substr($uuid, 1, -1);

}


// 导出excel
function export_excel($fileName, $headArr, $data){

    vendor('PHPExcel.PHPExcel');
    vendor('PHPExcel.PHPExcel.IOFactory');

    //创建新的PHPExcel对象
    $objPHPExcel = new \PHPExcel();
    $objPHPExcel->getProperties();

    //设置表头
    $key = ord("A");
    
    $objActSheet = $objPHPExcel->getActiveSheet();
    $headerStyle=$objActSheet->getStyle( 'A1:'.get_english_letters(count($headArr)-1).'1');
    $headerStyle->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
    $headerStyle->getFill()->getStartColor()->setARGB('064585');
    

    $headerFont = $headerStyle->getFont();
    $headerFont->setName('黑体');
    $headerFont->setSize(13);
    $headerFont->setBold();
    $headerFont->getColor()->setARGB('FFFFF3');
    
    $objPHPExcel->setActiveSheetIndex(0);//设置列宽

    //ob_end_clean(); //ryan190303 moved down
    foreach($headArr as $k => $v)
    {
        $objActSheet->getColumnDimension(get_english_letters($k))->setWidth($v['width']);
        
        $colum = chr($key);
        $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.'1', $v['name']);
        $key += 1;
    }
     
    $column = 2;
    foreach($data as $key => $rows)
    { 
        //行写入
        $span = ord("A");
        foreach($rows as $keyName=>$value)
        {// 列写入
            $j = chr($span);
            $objActSheet->setCellValue($j.$column, $value);
            $span++;
        }
        $column++;
    }

    $fileName = iconv("utf-8", "gb2312", $fileName);
    //重命名表
    $objPHPExcel->getActiveSheet()->setTitle('Simple');
    //设置活动单指数到第一个表,所以Excel打开这是第一个表
    $objPHPExcel->setActiveSheetIndex(0);

    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

    ob_end_clean();  //清空缓存
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
    header("Content-Type:application/force-download");
    header("Content-Type:application/vnd.ms-execl");
    header("Content-Type:application/octet-stream");
    header("Content-Type:application/download");
    header("Content-Disposition:attachment;filename={$fileName}");
    header("Content-Transfer-Encoding:binary");
    
    $objWriter->save('php://output');
    exit;

}

function get_english_letters($key){
    $data=array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'G', 'K', 'L', 'M', 'N', 'O', 'P', 'K', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',);
    return $data[$key];
}

# 将二维数组的键名修改为指定的键值
function change_array_key_by_value(array $input, $index_key)
{
    $data = array();
    foreach($input as $entry) {
        $data[$entry[$index_key]] = $entry;
    }
    return $data;
}

function GetIP(){
    if(!empty($_SERVER["HTTP_CLIENT_IP"])){
      $cip = $_SERVER["HTTP_CLIENT_IP"];
    }
    elseif(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
      $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
    }
    elseif(!empty($_SERVER["REMOTE_ADDR"])){
      $cip = $_SERVER["REMOTE_ADDR"];
    }
    else{
      $cip = "无法获取！";
    }
    return $cip;
}


// 根据结果计算性格类型,取最大值,可以有并列
function calcDISC($params)
{
    $max = max($params);
    $character = [];
    if($max <= 0) {
        return $character;
    }
    while(array_search($max, $params))
    {
        $k = array_search($max, $params);
        $character[] = $k;
        unset($params[$k]);
    }
    return $character;
}